package gang.org.springframework.framework.web.servlet;

import gang.org.springframework.framework.context.GangApplicationContext;
import gang.org.springframework.framework.util.GangBeanFactoryUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：gang.chen
 * @Date：2023/10/13 9:38
 */
public class GangDispatcherServlet extends GangFrameworkServlet{


    private boolean detectAllHandlerAttributes = true;

    /**
     * initStrategies方法在请求前完成初始化
     * */
    private List<GangHandlerMapping> handlerMappings;

    @Override
    public void onRefresh(GangApplicationContext context) {
        initStrategies(context);
    }

    private void initStrategies(GangApplicationContext context) {

        //TODO

        initHandlerMappings(context);

    }

    /**
     * 这里一共取到5个HandlerMapping,别分是
     * 1，“welcomePageHandlerMapping” -> WelcomePageHandlerMapping
     * 2，"requestMappingHandlerMapping" -> RequestMappingHandlerMapping
     * 3，“beanNameHandlerMapping” -> BeanNameUrlHandlerMapping
     * 4，“routerFunctionMapping” -> RouterFunctionHandlerMapping
     * 5，“resourceHandlerMapping” -> SimpleUrlHandlerMapping
     * */
    private void initHandlerMappings(GangApplicationContext context) {
        this.handlerMappings = null;

        if (this.detectAllHandlerAttributes)
        {
            Map<String, GangHandlerMapping> matchingBeans = GangBeanFactoryUtils.beansOfTypeIncludingAncestors(context, GangHandlerMapping.class, true, false);

            if (!matchingBeans.isEmpty())
            {
                this.handlerMappings = new ArrayList<>(matchingBeans.values());

                //TODO
            }
        }

        //TODO if (this.handlerMappings == null)


        //TODO for(HandlerMapping mapping : this.handlerMappings)
    }

    @Override
    public void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //TODO request process begin

        try{
            doDispatch(request,response);
        }
        finally {
            //TODO
        }
    }

    public void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpServletRequest processedRequest = request;

        GangHandlerExecutionChain mappedHandler = null;
        try
        {
            //TODO mv

            //TODO dispatchException

            try {

                //TODO processedRequest

                //TODO multipartRequestParsed

                mappedHandler = getHandler(processedRequest);

            }
            catch (Exception ex)
            {
                //TODO
            }
        }
        catch (Exception ex)
        {
            //TODO
        }
        finally
        {
            //TODO
        }
    }

    public GangHandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        if (this.handlerMappings !=null) {
            for (GangHandlerMapping mapping : this.handlerMappings) {

                //TODO mapping.getHandler(request)
            }
        }
        return null;
    }
}
